/* /home/ante/Desktop/Projects/SleekPHP/Sites/SleekBase/Modules/Base/JS/1-SleekPHP.js */
var SleekPHP = {
	modules: [], 
	styles: [], 

	run: function () {
		SleekPHP.runModules();
		SleekPHP.runStyles();
	}, 

	runModules: function () {
		for (var module in SleekPHP.modules) {
			// Work out the HTML-ID based on the module-name (RecentArticles == recent-articles)
			var id		= module.replace(/([A-Z])/g, '-$1').toLowerCase();
				id		= id.substring(0, 1) == '-' ? id.substring(1) : id;
			var mod		= jQuery('#' + id);

			// Only run modules that are used and don't run ajax-run-modules
			if ( 
					mod.length && 
					typeof(SleekPHP.modules[module]) != 'undefined' && 
					typeof(SleekPHP.modules[module].run) == 'function' 
			) {

				SleekPHP.modules[module].run(mod);
			}
		}
	}, 

	runStyles: function () {
		for (var style in SleekPHP.styles) {
			if ( 
				typeof(SleekPHP.styles[style]) != 'undefined' && 
				typeof(SleekPHP.styles[style].run) == 'function' 
			) {
				SleekPHP.styles[style].run();
			}
		}
	}
};

$(SleekPHP.run);

/* /home/ante/Desktop/Projects/SleekPHP/Sites/SleekBase/Modules/Base/JS/jQuery.equalHeight.js */
/***
@title:
Equal Height

@version:
2.1

@author:
Andreas Lagerkvist

@date:
2008-09-16

@url:
http://jquery.andreaslagerkvist.com/equal-height/

@license:
http://creativecommons.org/licenses/by/3.0/

@copyright:
2008 Andreas Lagerkvist (andreaslagerkvist.com)

@requires:
jquery

@does:
This plug-in makes HTML-elements equal height by adjusting their min-height CSS properties. Of course IE6 has no clue what min-height means so the plug-in uses height for that crappy old dinosaur.

@howto:
jQuery('#content, #secondary-content').equalHeight(); would make the elements with IDs 'content' and 'secondary-content' equal height.

@exampleHTML:
<p>Hi</p>

<p>Hi<br />Bye!</p>

@exampleJS:
jQuery('#jquery-equal-height-example p').equalHeight();
***/
(function ($) {
	$.fn.equalHeight = function () {
		var height		= 0;
		var maxHeight	= 0;

		// Store the tallest element's height
		this.each(function () {
			height		= $(this).outerHeight();
			maxHeight	= (height > maxHeight) ? height : maxHeight;
		});

		// Set element's min-height to tallest element's height
		return this.each(function () {
			var t			= $(this);
			var minHeight	= maxHeight - (t.outerHeight() - t.height());
			var property	= $.browser.msie && $.browser.version < 7 ? 'height' : 'min-height';

			t.css(property, minHeight + 'px');
		});
	};
})(jQuery);

/* /home/ante/Desktop/Projects/SleekPHP/Sites/SleekBase/Modules/Base/JS/jQuery.fixedIfNeeded.js */
/**
 * ...
 *
 */
(function ($) {
	$.fn.fixedIfNeeded = function (stopBefore) {
		return this.each(function () {
			var el = $(this);
			var elHeight = el.outerHeight();
			var offset = el.offset();
			var stopBeforeOffset = stopBefore ? $(stopBefore).offset() : false;
			var stopBeforeHeight = stopBefore ? $(stopBefore).outerHeight() : false;
			var oldCSS = {
				float: el.css('float'), 
				margin: el.css('margin'), 
				position: el.css('position'), 
				left: el.css('left'), 
				top: el.css('top')
			};
			var newCSS = {
				float: 'none', 
				margin: 0, 
				position: 'fixed', 
				left: offset.left + 'px', 
				top: 0
			};
			var absoluteCSS = {
				float: 'none', 
				margin: 0, 
				position: 'absolute', 
				left: offset.left + 'px', 
				top: 0
			};

			if (false) {
				$(this).css(newCSS);
			}

			var positionEl = function () {
				var scrollTop = $(window).scrollTop();

				if (scrollTop > offset.top) {
					if (stopBefore) {
						var limit = stopBeforeOffset.top + stopBeforeHeight;
						var top = scrollTop + elHeight;

						if (top < limit) {
							el.css(newCSS);
						}
						else {
							absoluteCSS.top = (stopBeforeOffset.top + stopBeforeHeight - elHeight) + 'px';

							el.css(absoluteCSS);
						}
					}
					else {
						el.css(newCSS);
					}
				}
				else {
					el.css(oldCSS);
				}
			};

			positionEl();

			$(window).scroll(positionEl);
		});
	};
})(jQuery);

/* /home/ante/Desktop/Projects/SleekPHP/Sites/SleekBase/Modules/Base/JS/jQuery.generateDocumentOutline.js */
/**
 * ...
 *
 */
(function ($) {
	$.fn.generateDocumentOutline = function (where) {
		// Generates the outline
		var generateOutline = function (headings) {
			var html = '<ul>';

			headings.each(function (i) {
				var heading = $(this);
				var currLevel = heading[0].nodeName.substr(1);
				var name = heading.text().replace(/ /g, '-').toLowerCase();

				heading.attr('id', 'jquery-generate-document-outline-' + name + '-'  + currLevel + '-' + i);

				html += '<li><a href="#jquery-generate-document-outline-' + name + '-'  + currLevel + '-' + i + '">' + heading.text() + '</a>';

				var nextHeadings = heading.nextUntil('h' + currLevel, 'h' + (parseInt(currLevel, 10) + 1));

				if (nextHeadings.length) {
					html += generateOutline(nextHeadings);
				}

				html += '</li>';
			});

			html += '</ul>';

			return html;
		};

		return this.each(function () {
			var doc = $(this);

			$(where).append(generateOutline(doc.find(doc.find(':header')[0].nodeName)));
		});
	};
})(jQuery);

/* /home/ante/Desktop/Projects/SleekPHP/Sites/SleekBase/Modules/Base/JS/jQuery.threeDCircle.js */
/**
 * ...
 *
 */
(function ($) {
	$.fn.threeDCircle = function (conf) {
		var toRadian = Math.PI / 180;
		var toDegree = 180 / Math.PI;

		// Shortcut function to apply CSS transform to all browsers
		var transform = function (el, ts) {
			var t = 'rotateX(' + (ts.rotateX || 0) + ')' + 
					'rotateY(' + (ts.rotateY || 0) + ')' + 
					'rotateZ(' + (ts.rotateZ || 0) + ')' + 
					'translateX(' + (ts.translateX || 0) + ')' + 
					'translateY(' + (ts.translateY || 0) + ')' + 
					'translateZ(' + (ts.translateZ || 0) + ')';

			el.css({
				webkitTransform: t, 
				mozTransform: t, 
				oTransform: t, 
				msTransform: t, 
				transform: t
			});
		};

		// Be nice
		return this.each(function () {
			var mover		= $(this).addClass('jquery-3d-circle-stage').find('> *').eq(0).addClass('jquery-3d-circle-mover');
			var circle		= mover.find('> *').eq(0).addClass('jquery-3d-circle');
			var items		= circle.find('> *');
			var numItems	= items.length;
			var currItem	= 0;
			var currAngle	= 0;

			// Make everything equal height
			items.equalHeight();

			var itemWidth = items.eq(0).outerWidth();
			var itemHeight = items.eq(0).outerHeight();

			circle.css({
				width: itemWidth + 'px', 
				height: itemHeight + 'px'
			});

			// And move the "mover" down z
			var radie = (itemWidth * numItems) / Math.PI / 2;

			transform(mover, {translateZ: '-' + radie + 'px'});

			// Position all the items
			var increase = Math.ceil(360 / numItems);
			var angle = 0;

			items.each(function (i) {
				var item		= $(this).addClass('jquery-3d-circle-item');

				transform(item, {
					translateZ:	radie + 'px', 
					rotateY:	angle + 'deg'
				});

				angle += increase;
			});

			// Goes to a specific item
			var gotoItem = function (nextItem) {
				if (currItem < nextItem) {
					var diffRight	= nextItem - currItem;
					var diffLeft	= currItem + (numItems - nextItem);
				}
				else {
					var diffRight	= (numItems - currItem) + nextItem;
					var diffLeft	= currItem - nextItem;
				}

				if (diffLeft < diffRight) {
					currAngle -= diffLeft * increase;
				}
				else {
					currAngle += diffRight * increase;
				}

				currItem = nextItem;

				transform(circle, {rotateY: -currAngle + 'deg'});

				// Change selected links
				$('a').removeClass('jquery-3d-circle-selected');
				$('a[href="#' + items.eq(currItem).attr('id') + '"]').addClass('jquery-3d-circle-selected');
			};

			// Rotate when clicking the items
			items.click(function () {
				gotoItem(items.index(this));
			});

			// Rotate the wrapper when links pointing to certain sections within it are clicked
			$('a').live('click', function () {
				var id = $(this).attr('href');
				var clicked = items.filter(id);

				if (clicked.length) {
					gotoItem(items.index(clicked));
				}

				return false;
			});
		});
	};
})(jQuery);

/* /home/ante/Desktop/Projects/SleekPHP/Sites/SleekBase/Modules/Base/JS/jQuery.togglableFormFields.js */
/**
 * ...
 *
 */
(function ($) {
	$.fn.togglableFormFields = function () {
		return this.each(function () {
			var form = $(this).addClass('jquery-togglable-form-fields');
			var labels = form.find('label[for]');

			labels.each(function () {
				var label = $(this).addClass('jquery-togglable-form-fields');
				var input = form.find('#' + label.attr('for')).addClass('jquery-togglable-form-fields');

				label.toggle(function () {
					label.addClass('jquery-togglable-form-fields-open');
					input.addClass('jquery-togglable-form-fields-open');

					// If all are open
					if (!labels.filter(':not(.jquery-togglable-form-fields-open)').length) {
						form.addClass('jquery-togglable-form-fields-all-open');
					}

					setTimeout(function () {
						input.focus();
					}, 1000);
				}, function () {
					label.removeClass('jquery-togglable-form-fields-open');
					input.removeClass('jquery-togglable-form-fields-open');
					form.removeClass('jquery-togglable-form-fields-all-open');
				});
			});
		});
	};
})(jQuery);

/* /home/ante/Desktop/Projects/SleekPHP/Sites/SleekBase/Modules/Search/JS/Search.js */
SleekPHP.modules.Search = {
	run: function () {
	//	$('#q').liveSearch({
	//		url: url('SearchResults') + '&q='
	//	});
	}
};

/* /home/ante/Desktop/Projects/SleekPHP/Sites/SleekCMS/Styles/SleekCMS/JS/SleekCMS.js */
SleekPHP.styles.SleekCMS = {
	run: function () {
		if ($('#login-page').length) {
			this.loginNavigation();
			this.emptyLoginBoxes();
			this.themeSwitcher();

			$('#circle-stage').threeDCircle();
		}

		$('#admin-navigation form').togglableFormFields();
	}, 

	themeSwitcher: function () {
		var html = '<nav id="login-theme-switcher"><h2>Change theme</h2><ul>';
		var themes = [
			'osx', 
			'bright-and-shiny', 
			'planet-sky-bright', 
			'bright-fantasy-worldwide', 
			'too-bright-grass', 
			'beautiful-sea', 
			'bright-blue-water', 
			'life', 
			'clown-fish'
		];

		for (var i = 0; i < themes.length; i++) {
			var theme = themes[i];
			var title = theme.charAt(0).toUpperCase() + theme.substr(1).replace(/-/g, ' ');

			html += '<li><a href="#' + theme + '">' + title + '</a></li>';
		}

		html += '</ul></nav>';

		$(html).appendTo('body').find('a').click(function () {
			$('a').removeClass('login-theme-selected');

			$('body').attr('class', $(this).addClass('login-theme-selected').attr('href').substr(1));

			return false;
		}).eq(0).addClass('login-theme-selected');
	}, 

	emptyLoginBoxes: function () {
		for (var i = 0; i < 6; i++) {
			$('<div id="login-filler-' + i + '"/>').appendTo('#circle');
		}
	}, 

	loginNavigation: function () {
		var html = '<nav id="login-navigation"><ul>';

		$('#circle > *[id]').each(function () {
			var section = $(this);
			var title = section.find('h2').eq(0).text();
			var id = section.attr('id');

			html += '<li><a href="#' + id + '">' + title + '</a></li>';
		});

		html += '</ul></nav>';

		$(html).insertBefore('#circle-stage').find('a').eq(0).addClass('jquery-3d-circle-selected');
	}
};

